﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClipperHouse.jQuery.Base;

namespace ClipperHouse.jQuery
{
	/// <summary>
	/// jQuery toggle() effect. See http://docs.jquery.com/Effects/toggle
	/// </summary>
	public class Toggle : JQueryWithSpeed
	{
		private const string switchParamName = "Switch";

		private bool originalIsCallbackEnabled
		{
			get;
			set;
		}

		public Toggle()
		{
			this.SpeedIfRequired = "0";

			this.Params.Add(switchParamName, String.Empty);
			originalIsCallbackEnabled = this.IsCallbackEnabled;
		}

		public override string jQueryMethodName
		{
			get { return "toggle"; }
		}

		/// <summary>
		/// Toggle elements based upon the switch (true shows elements, false hides elements). 
		/// See http://docs.jquery.com/Effects/toggle#switch 
		/// Note: using Switch precludes the use of other parameters such as Speed. Those other parameters will be nullfied if Switch is specified.
		/// </summary>
		public bool? Switch
		{
			get
			{
				bool switchAsBool;

				if (Params[switchParamName] != null && Boolean.TryParse(Params[switchParamName].ToString(), out switchAsBool))
				{
					return switchAsBool;
				}
				else
				{
					return null;
				}
			}
			set
			{
				if (value != null)
				{
					Params[switchParamName] = value.ToString();
				}
				else
				{
					Params[switchParamName] = String.Empty;
				}

				// Switch is an exclusive param -- other params are nullified if specified
				if (!String.IsNullOrEmpty(Params[switchParamName]))
				{
					base.AllowedParams.Add(switchParamName);
					this.IsCallbackEnabled = false;
				}
				else
				{
					base.AllowedParams.RemoveAll(p => p == switchParamName);
					this.IsCallbackEnabled = originalIsCallbackEnabled;
				}

			}
		}
	}
}
